package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.vo.StrategyStatisVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;

    @GetMapping("/themes")
    public Object themes(){
        // 查询所有的攻略主题
        return JsonResult.success(strategyThemeService.list());
    }

    @PostMapping("/commentThumb")
    @RequireLogin
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){
        // 点赞/取消点赞
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }

    @GetMapping("/detail")
    public Object detail(String id){
        // 增加阅读数量 可设置每次进入这个页面增加的次数，但是我暂时不设置，默认为1
        strategyStatisVoRedisService.increaseViewnum(id);
        // 通过 攻略id 查询攻略明细
        Strategy strategy = strategyService.get(id);
        return JsonResult.success(strategy);
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        // 根据 目的id 查询该目的地下的 攻略明细
        Page<Strategy> strategies = strategyService.queryByDestId(qo);
        // 通过 攻略id 查询攻略明细
        return JsonResult.success(strategies);
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        // 查询指定攻略的所有评论，进行分页
        Page<StrategyComment> strategyComments = strategyCommentService.query(qo);
        return JsonResult.success(strategyComments);
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        // 查询指定攻略的统计对象
        StrategyStatisVO vo = strategyStatisVoRedisService.getStrategyStaticVO(sid);
        return JsonResult.success(vo);
    }

    @PostMapping("/addComment")
    @RequireLogin
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo){
        // get UserInfo 的属性值，set strategy 中相同属性中。
        BeanUtils.copyProperties(userInfo,strategyComment);
        strategyComment.setUserId(userInfo.getId());
        // 给攻略添加一条评论
        strategyCommentService.addComment(strategyComment);
        // 添加评论完，评论数加一
        strategyStatisVoRedisService.increaseReplynum(strategyComment.getStrategyId());
        return JsonResult.success();
    }

    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid,@UserParam UserInfo userInfo){
        // 收藏/取消收藏
        boolean ret = strategyStatisVoRedisService.increaseFavor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

    @PostMapping("/strategyThumbup")
    @RequireLogin
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        // 置顶
        boolean ret = strategyStatisVoRedisService.increaseThumb(sid,userInfo.getId());
        return JsonResult.success(ret);
    }


}
